package service.impl;

import dao.BookInfoDao;
import dao.impl.BookInfoDaoImpl;
import entity.BookInfo;
import service.BookInfoService;
import util.PageHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * User: key
 * ClassName:Book_InfoServiceImpl
 * Date: 2023/5/6
 * Time: 15:07
 * description:
 */
public class BookInfoServiceImpl implements BookInfoService {
    private BookInfoDao bd = new BookInfoDaoImpl();
    @Override
    public String borrow(int bookId) {
        try {
            if(bookId<1){
                return "没有这本书，请重试";
            }
            BookInfo info = bd.getBookById(bookId);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    public PageHelper getByPageWithKeyWord(int pageNum, int pageSize, String bookType, String bookName, String isBorrow) {
        try {
            String sql = "select " +
                    "book_id 'bookId',book_code 'bookCode'," +
                    "book_name 'bookName',book_type 'bookType' ," +
                    "book_author 'bookAuthor',publish_press 'publishPress'," +
                    "publish_date 'publishDate',is_borrow 'isBorrow' ,t.type_name 'typeName'" +
                    "from book_info b inner join book_type t on b.book_type=t.id where 1=1";
            String countSql = "select count(1) from book_info where 1 = 1 ";
            List args = new ArrayList();
            if(bookType != null && !bookType.equals("")){
                sql += " and book_type = ?";
                countSql += " and book_type = ?";
                args.add(bookType);
            }
            if(bookName != null && !bookName.equals("")){
                sql += " and book_name like ?";
                countSql += " and book_name like ?";
                args.add(" %"+bookName+"%");
            }
            if(isBorrow != null && !isBorrow.equals("")){
                sql += " and is_borrow = ?";
                countSql += " and is_borrow = ?";
                args.add(isBorrow);
            }
            sql += " limit ?,?";
            Long count = bd.getCount(countSql,args.toArray());
            PageHelper<BookInfo> helper = new PageHelper<>(pageNum,pageSize,count.intValue());
            args.add(helper.getStartRow());
            args.add(helper.getPageSize());
            List<BookInfo> list = bd.getBookByKeyWord(sql,args.toArray());
            helper.setList(list);
            return helper;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
